package com.example.parkingproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.parkingproject.entity.ParkingData;
import com.example.parkingproject.mapper.ParkingMapper;
import com.example.parkingproject.result.Result;
import com.example.parkingproject.service.ParkingService;
import com.example.parkingproject.vo.ParkingDayData;
import com.example.parkingproject.vo.ParkingNumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Slf4j
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, ParkingData> implements ParkingService {

    @Autowired
    private ParkingMapper parkingMapper;

    /**
     * 获取本月内的停车数据
     * @return
     */
    @Override
    public Result getParkingData() {
        //获取今日的登记的数据
        Integer todayNum = parkingMapper.getTodayNum();
        //获取本周的数据
        Integer weekNum = parkingMapper.getWeekNum();
        //获取本月的数据
        Integer monthNum = parkingMapper.getMonthNum();

        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,todayNum);
        map.put(1,weekNum);
        map.put(2,monthNum);

        return Result.success(map);
    }


    /**
     * 获取近七天的停车数据
     * @return
     */
    @Override
    public Result getSevenDaysParkingData() {
        // 获取当前日期
        LocalDate date = LocalDate.now();
        List<ParkingDayData> list = new ArrayList<>();
        for(int i = 0; i < 7; i++){
            Integer num = parkingMapper.getOneDayNum(date);
            ParkingDayData dayData = new ParkingDayData();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
            String monthAndDay = date.format(formatter);

            dayData.setDate(monthAndDay);
            dayData.setNum(num);
            list.add(dayData);
            date = date.minusDays(1);
        }
        return Result.success(list);
    }

    @Override
    public Result getMonthParkingDurationData() {

        List<ParkingNumber> parkingNumberList = new ArrayList<>();
        parkingNumberList = parkingMapper.getParkingNumberList();
        Integer oneDay = 0;
        Integer oneToTreeDays = 0;
        Integer treeToSevenDays = 0;
        Integer sevenDays = 0;
        for (ParkingNumber parkingNumber : parkingNumberList) {
            if(parkingNumber.getNum() == 1){
                oneDay++;
            }
            if(parkingNumber.getNum() > 1 && parkingNumber.getNum() <= 3){
                oneToTreeDays++;
            }
            if(parkingNumber.getNum() > 3 && parkingNumber.getNum() <= 7){
                treeToSevenDays++;
            }
            if(parkingNumber.getNum() > 7){
                sevenDays++;
            }
        }

        List<ParkingNumber> list = new ArrayList<>();
        list.add(new ParkingNumber("1天",oneDay));
        list.add(new ParkingNumber("1-3天",oneToTreeDays));
        list.add(new ParkingNumber("3-7天",treeToSevenDays));
        list.add(new ParkingNumber("7天以上",sevenDays));
        return Result.success(list);
    }
}
